<template>
  <div>
    <MyTable :list="list">
      <template #heade>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row, ind }">
        <td>{{ ind + 1 }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
            class="tag-input form-control"
            style="width: 100px"
            v-if="row.inputVisible"
            v-model="row.inputValue"
            type="text"
            @keyup.enter="addTage(row)"
            @blur="row.inputVisible = false"
            @keyup.esc="row.inputValue = ''"
            v-fff
          />
          <button
            v-else
            class="btn btn-primary btn-sm add-tag"
            @click="row.inputVisible = true"
          >
            +Tag
          </button>
          <div>
            <span
              class="badge badge-warning"
              v-for="(item1, i) in row.tags"
              :key="i"
              >{{ item1 }}</span
            >
          </div>
        </td>
        <td>
          <button class="btn btn-danger btn-sm" @click="removeList(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get(
        "https://www.escook.cn/api/goods"
      );
      this.list = res.data;
      console.log(this.list);
    },
    removeList(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },

    addTage(row) {
      // if (this.list[index].inputValue.trim().length === 0) {
      //   this.list[index].inputValue = "";
      //   return alert("请输入内容");
      // }
      if (row.inputValue.trim().length === 0) {
        row.inputValue =''
        return alert("请输入内容");
      }

      // this.list[index].tags.push(this.list[index].inputValue.trim());
      // this.list[index].inputValue = "";
      // this.list[index].inputVisible = false;
      row.tags.push(row.inputValue.trim());
      (row.inputValue = ""), (row.inputVisible = false);
    },
  },
  directives: {
    fff: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>

<style scoped>
span {
  display: inline-block;
  margin-left: 5px;
}
</style>
